package co.gov.mintic.inmueblesdisponibles.android;

import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import co.gov.mintic.inmueblesdisponibles.vo.Inmueble;

/**
 * Clase encargada de visualizar los inmuebles del resultado de la busqueda
 * @author Benjamin
 *
 */
public class CustomListAdapter extends BaseAdapter {

	private Activity activity;
	private int id;
	private List<Inmueble> items;
	private HashMap<Long, Inmueble> inmuebles;

	public CustomListAdapter(Activity activity, int textViewResourceId, List<Inmueble> list) {
		id = textViewResourceId;
		items = list;
		inmuebles = new HashMap<Long, Inmueble>();
		for (Inmueble inmueble : list) {
			inmuebles.put(inmueble.getId(), inmueble);
		}
		
		this.activity = activity;
	}

	@Override
	public View getView(int position, View v, ViewGroup parent) {
		View mView = v;
		if (mView == null) {
			LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			mView = inflater.inflate(R.layout.custom_list, null);
		}

		TextView text = (TextView) mView.findViewById(R.id.textView);

		if (items.get(position) != null) {
			text.setTextColor(Color.BLACK);
			text.setText(items.get(position).toString());
			text.setBackgroundColor(Color.TRANSPARENT);
		}

		return mView;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return items.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return items.get(position).getId();
	}
	
	public Inmueble getInmueble(long id) {
		return inmuebles.get(id);
	}
}
